package com.dataknife.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.dataknife.util.StringUtil;


public final class DateUtil
{
  private static final Logger logger = Logger.getLogger(DateUtil.class);
  
  @SuppressWarnings("deprecation")
  public static Date parseRepeatTime(String time){
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    try {
      date = sdf.parse(time);
    } catch (ParseException e) {
      logger.error("configuration time occur error ",e);
    }
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    calendar.set(year, month, day, date.getHours(), date.getMinutes(), date.getSeconds());
    return calendar.getTime();
  }
  
  public static String getYesterday(){
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE,   -1);
    String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    return yesterday;
  }
  
  public static String getToday(){
    Calendar cal = Calendar.getInstance();
    String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    return yesterday;
  }
  
  public static String getLasthour(){
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR_OF_DAY,   -1);
    String yesterday = new SimpleDateFormat("yyyy-MM-dd HH").format(cal.getTime());
    return yesterday;
  }
  
  public static String getHour(){
    Calendar cal = Calendar.getInstance();
    String hour = new SimpleDateFormat("yyyy-MM-dd HH").format(cal.getTime());
    return hour;
  }
  
  public static String getLastWeek(){
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.WEEK_OF_YEAR,   -1);
    String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    return yesterday;
  }
  
  public static String getWeek(){
    Calendar cal = Calendar.getInstance();
    String week = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    return week;
  }
  
  public static String getLastMonth(){
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH, -1);
    String lastmonth = new SimpleDateFormat("yyyy-MM").format(cal.getTime());
    return lastmonth;
  }
  
  public static String getMonth(){
    Calendar cal = Calendar.getInstance();
    String month = new SimpleDateFormat("yyyy-MM").format(cal.getTime());
    return month;
  }
  
  public static String getLastYear(){
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, -1);
    String lastyear = new SimpleDateFormat("yyyy").format(cal.getTime());
    return lastyear;
  }
  
  public static String getYear(){
    Calendar cal = Calendar.getInstance();
    String year = new SimpleDateFormat("yyyy").format(cal.getTime());
    return year;
  }
  
  public static String getCurrentTimeStr() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date = sdf.format(new Date());
    return date;
  }

  public static String strToDate(String date)
  {
    if (StringUtil.isNullOrBlank(date)) {
      return getCurrentTimeStr();
    }
    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    int index = date.indexOf(".");
    if (index != -1) {
      date = date.substring(0, index);
    }
    if (date.length() < 13){ 
      int size = 13 - date.length();
      for (int i=0;i<size;i++) {
        date += "0";
      }
    }
    String temp = simpledateformat.format(Long.valueOf(date));
    return temp;
  }
  
  public static Date addDay(Date date, int num){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, num);
    return cal.getTime();
  }

  public static void main(String... args){
    String oneHoursAgoTime = "";
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.WEEK_OF_YEAR, -1);
    oneHoursAgoTime = new SimpleDateFormat("yyyy-MM-dd")
            .format(cal.getTime());

    System.out.println(oneHoursAgoTime);
  }
}
